1 Effect of UPSTM-Based Decorrelation on Feature Discovery

1.0.1 Loading the libraries

library("FRESA.CAD")
library(readxl)
library(igraph)
library(umap)
library(tsne)
library(entropy)

op <- par(no.readonly = TRUE)
pander::panderOptions('digits', 3)
pander::panderOptions('table.split.table', 400)
pander::panderOptions('keep.trailing.zeros',TRUE)

1.1 Material and Methods

Data Source https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6960825/

Mohino-Herranz I, Gil-Pita R, Rosa-Zurera M, Seoane F. Activity Recognition Using Wearable Physiological Measurements: Selection of Features from a Comprehensive Literature Study. Sensors (Basel). 2019 Dec 13;19(24):0. doi: 10.3390/s19245524. PMID: 31847261; PMCID: PMC6960825.

1.2 The Data

Activitydata <- read.csv("~/GitHub/LatentBiomarkers/Data/ActivityData/data.txt", header=FALSE, stringsAsFactors=TRUE)
featNames <- read.table("~/GitHub/LatentBiomarkers/Data/ActivityData/Featurelabels.txt", quote="\"", comment.char="")
featNames <- as.character(t(featNames))
featNames <- str_replace_all(featNames,"\\(abs\\)","_A_")
featNames[duplicated(featNames)] <- paste(featNames[duplicated(featNames)],"D",sep="_")

rep_ID <- numeric(nrow(Activitydata))
ctr <- 1
for (ind in c(1:(nrow(Activitydata)-1)))
{
  rep_ID[ind] <- ctr
  if (Activitydata$V1[ind] != Activitydata$V1[ind+1]) ctr <- 0;
  ctr <- ctr + 1
}
rownames(Activitydata) <- paste(Activitydata$V1,rep_ID,sep="_")
colnames(Activitydata) <- c("ID",featNames,"class")
Activitydata$rep <- rep_ID
  
tb <- table(Activitydata$class)

classes <- c("Neu","Emo","Men","Phy")
names(classes) <- names(tb)
Activitydata$class <- classes[as.character(Activitydata$class)]
table(Activitydata$class)
#> 
#>  Emo  Men  Neu  Phy 
#> 1120 1120 1120 1120



ID_class <- paste(Activitydata$ID,Activitydata$class)
IDCLASS <- unique(ID_class)
theclass <- Activitydata$class[!duplicated(ID_class)]
theIDs <- Activitydata$ID[!duplicated(ID_class)]

ActivitydataAvg <- NULL
for (id in IDCLASS)
{
  ActivitydataAvg <- rbind(ActivitydataAvg,apply(Activitydata[ID_class==id,featNames],2,mean))
}
colnames(ActivitydataAvg) <- featNames
rownames(ActivitydataAvg) <- IDCLASS
ActivitydataAvg <- as.data.frame(ActivitydataAvg)
ActivitydataAvg$class <- theclass
ActivitydataAvg$ID <- theIDs

table(ActivitydataAvg$class)
#> 
#> Emo Men Neu Phy 
#>  40  40  40  40


ActivitydataAvg <- subset(ActivitydataAvg, class=="Men" | class=="Emo")

ActivitydataAvg$class <- 1*(ActivitydataAvg$class == "Men")
table(ActivitydataAvg$class)
#> 
#>  0  1 
#> 40 40

1.2.0.1 Standarize the names for the reporting

studyName <- "Activity"
dataframe <- ActivitydataAvg
outcome <- "class"

TopVariables <- 10
thro <- 0.80
cexheat = 0.15

1.3 Generaring the report

1.3.1 Libraries

Some libraries

library(psych)
library(whitening)
library("vioplot")

1.3.2 Data specs

pander::pander(c(rows=nrow(dataframe),col=ncol(dataframe)-1))
rows col
80 534
pander::pander(table(dataframe[,outcome]))
0 1
40 40

varlist <- colnames(dataframe)
varlist <- varlist[varlist != outcome]

largeSet <- length(varlist) > 1500

1.3.3 Scaling the data

Scaling and removing near zero variance columns and highly co-linear(r>0.99999) columns


  ### Some global cleaning
  sdiszero <- apply(dataframe,2,sd) > 1.0e-16
  dataframe <- dataframe[,sdiszero]

  varlist <- colnames(dataframe)[colnames(dataframe) != outcome]
  tokeep <- c(as.character(correlated_Remove(dataframe,varlist,thr=0.99999)),outcome)
  dataframe <- dataframe[,tokeep]

  varlist <- colnames(dataframe)
  varlist <- varlist[varlist != outcome]


dataframe <- FRESAScale(dataframe,method="OrderLogit")$scaledData

1.4 The heatmap of the data


if (!largeSet)
{
  
  hm <- heatMaps(data=dataframe,
                 Outcome=outcome,
                 Scale=TRUE,
                 hCluster = "row",
                 xlab="Feature",
                 ylab="Sample",
                 srtCol=45,
                 srtRow=45,
                 cexCol=cexheat,
                 cexRow=cexheat
                 )
  par(op)
}

1.4.0.1 Correlation Matrix of the Data

The heat map of the data


if (!largeSet)
{

  par(cex=0.6,cex.main=0.85,cex.axis=0.7)
  #cormat <- Rfast::cora(as.matrix(dataframe[,varlist]),large=TRUE)
  cormat <- cor(dataframe[,varlist],method="pearson")
  cormat[is.na(cormat)] <- 0
  gplots::heatmap.2(abs(cormat),
                    trace = "none",
  #                  scale = "row",
                    mar = c(5,5),
                    col=rev(heat.colors(5)),
                    main = "Original Correlation",
                    cexRow = cexheat,
                    cexCol = cexheat,
                     srtCol=45,
                     srtRow=45,
                    key.title=NA,
                    key.xlab="|Pearson Correlation|",
                    xlab="Feature", ylab="Feature")
  diag(cormat) <- 0
  print(max(abs(cormat)))
}

[1] 1

1.5 The decorrelation


DEdataframe <- IDeA(dataframe,verbose=TRUE,thr=thro)
#> 
#>  Included: 362 , Uni p: 0.01628202 , Uncorrelated Base: 44 , Outcome-Driven Size: 0 , Base Size: 44 
#> 
#> 
 1 <R=1.000,r=0.975,N=  265>, Top: 47( 2 )[ 1 : 47 Fa= 45 : 0.975 ]( 45 , 189 , 0 ),<|>Tot Used: 234 , Added: 189 , Zero Std: 0 , Max Cor: 1.000
#> 
 2 <R=1.000,r=0.975,N=  265>, Top: 32( 12 )[ 1 : 32 Fa= 77 : 0.975 ]( 32 , 94 , 45 ),<|>Tot Used: 257 , Added: 94 , Zero Std: 0 , Max Cor: 1.000
#> 
 3 <R=1.000,r=0.975,N=  265>, Top: 15( 1 )[ 1 : 15 Fa= 92 : 0.975 ]( 15 , 22 , 77 ),<|>Tot Used: 257 , Added: 22 , Zero Std: 0 , Max Cor: 1.000
#> 
 4 <R=1.000,r=0.950,N=  114>, Top: 49( 1 )[ 1 : 49 Fa= 110 : 0.950 ]( 47 , 59 , 92 ),<|>Tot Used: 282 , Added: 59 , Zero Std: 0 , Max Cor: 0.997
#> 
 5 <R=0.997,r=0.949,N=  114>, Top: 9( 1 )[ 1 : 9 Fa= 114 : 0.949 ]( 9 , 10 , 110 ),<|>Tot Used: 282 , Added: 10 , Zero Std: 0 , Max Cor: 0.984
#> 
 6 <R=0.984,r=0.892,N=   92>, Top: 33( 1 )[ 1 : 33 Fa= 123 : 0.892 ]( 32 , 47 , 114 ),<|>Tot Used: 302 , Added: 47 , Zero Std: 0 , Max Cor: 0.996
#> 
 7 <R=0.996,r=0.898,N=   92>, Top: 8( 1 )[ 1 : 8 Fa= 126 : 0.898 ]( 8 , 8 , 123 ),<|>Tot Used: 303 , Added: 8 , Zero Std: 0 , Max Cor: 0.989
#> 
 8 <R=0.989,r=0.844,N=   79>, Top: 34( 1 )[ 1 : 34 Fa= 133 : 0.844 ]( 33 , 39 , 126 ),<|>Tot Used: 314 , Added: 39 , Zero Std: 0 , Max Cor: 0.983
#> 
 9 <R=0.983,r=0.842,N=   79>, Top: 9( 2 )[ 1 : 9 Fa= 136 : 0.842 ]( 9 , 11 , 133 ),<|>Tot Used: 314 , Added: 11 , Zero Std: 0 , Max Cor: 0.872
#> 
 10 <R=0.872,r=0.800,N=   79>, Top: 25( 1 )[ 1 : 25 Fa= 143 : 0.800 ]( 25 , 31 , 136 ),<|>Tot Used: 317 , Added: 31 , Zero Std: 0 , Max Cor: 0.853
#> 
 11 <R=0.853,r=0.800,N=   79>, Top: 4( 1 )[ 1 : 4 Fa= 145 : 0.800 ]( 4 , 4 , 143 ),<|>Tot Used: 317 , Added: 4 , Zero Std: 0 , Max Cor: 0.808
#> 
 12 <R=0.808,r=0.800,N=    2>, Top: 1( 1 )[ 1 : 1 Fa= 145 : 0.800 ]( 1 , 1 , 145 ),<|>Tot Used: 317 , Added: 1 , Zero Std: 0 , Max Cor: 0.798
#> 
 13 <R=0.798,r=0.800,N=    0>
#> 
 [ 13 ], 0.7983171 Decor Dimension: 317 Nused: 317 . Cor to Base: 188 , ABase: 18 , Outcome Base: 0 
#> 
varlistc <- colnames(DEdataframe)[colnames(DEdataframe) != outcome]

pander::pander(sum(apply(dataframe[,varlist],2,var)))

616

pander::pander(sum(apply(DEdataframe[,varlistc],2,var)))

155

pander::pander(entropy(discretize(unlist(dataframe[,varlist]), 256)))

4.84

pander::pander(entropy(discretize(unlist(DEdataframe[,varlistc]), 256)))

2.6

1.5.1 The decorrelation matrix


if (!largeSet)
{

  par(cex=0.6,cex.main=0.85,cex.axis=0.7)
  
  UPSTM <- attr(DEdataframe,"UPSTM")
  
  gplots::heatmap.2(1.0*(abs(UPSTM)>0),
                    trace = "none",
                    mar = c(5,5),
                    col=rev(heat.colors(5)),
                    main = "Decorrelation matrix",
                    cexRow = cexheat,
                    cexCol = cexheat,
                   srtCol=45,
                   srtRow=45,
                    key.title=NA,
                    key.xlab="|Beta|>0",
                    xlab="Output Feature", ylab="Input Feature")
  
  par(op)
}

1.6 The heatmap of the decorrelated data

if (!largeSet)
{

  hm <- heatMaps(data=DEdataframe,
                 Outcome=outcome,
                 Scale=TRUE,
                 hCluster = "row",
                 cexRow = cexheat,
                 cexCol = cexheat,
                 srtCol=45,
                 srtRow=45,
                 xlab="Feature",
                 ylab="Sample")
  par(op)
}

1.7 The correlation matrix after decorrelation

if (!largeSet)
{

  cormat <- cor(DEdataframe[,varlistc],method="pearson")
  cormat[is.na(cormat)] <- 0
  
  gplots::heatmap.2(abs(cormat),
                    trace = "none",
                    mar = c(5,5),
                    col=rev(heat.colors(5)),
                    main = "Correlation after IDeA",
                    cexRow = cexheat,
                    cexCol = cexheat,
                     srtCol=45,
                     srtRow=45,
                    key.title=NA,
                    key.xlab="|Pearson Correlation|",
                    xlab="Feature", ylab="Feature")
  
  par(op)
  diag(cormat) <- 0
  print(max(abs(cormat)))
}

[1] 0.9964137

1.8 U-MAP Visualization of features

1.8.1 The UMAP based on LASSO on Raw Data

classes <- unique(dataframe[,outcome])
raincolors <- rainbow(length(classes))
names(raincolors) <- classes
datasetframe.umap = umap(scale(dataframe[,varlist]),n_components=2)
plot(datasetframe.umap$layout,xlab="U1",ylab="U2",main="UMAP: Original",t='n')
text(datasetframe.umap$layout,labels=dataframe[,outcome],col=raincolors[dataframe[,outcome]+1])

1.8.2 The decorralted UMAP


datasetframe.umap = umap(scale(DEdataframe[,varlistc]),n_components=2)
plot(datasetframe.umap$layout,xlab="U1",ylab="U2",main="UMAP: After IDeA",t='n')
text(datasetframe.umap$layout,labels=DEdataframe[,outcome],col=raincolors[DEdataframe[,outcome]+1])

1.9 Univariate Analysis

1.9.1 Univariate



univarRAW <- uniRankVar(varlist,
               paste(outcome,"~1"),
               outcome,
               dataframe,
               rankingTest="AUC")

100 : ECG_p_LF_mean 200 : IT_CCV_LF 300 : EDA_Original_mad_D




univarDe <- uniRankVar(varlistc,
               paste(outcome,"~1"),
               outcome,
               DEdataframe,
               rankingTest="AUC",
               )

100 : La_ECG_p_LF_mean 200 : La_IT_CCV_LF 300 : La_EDA_Original_mad_D

1.9.2 Final Table


univariate_columns <- c("caseMean","caseStd","controlMean","controlStd","controlKSP","ROCAUC")

##topfive
topvar <- c(1:length(varlist)) <= TopVariables
pander::pander(univarRAW$orderframe[topvar,univariate_columns])
  caseMean caseStd controlMean controlStd controlKSP ROCAUC
ECG_hrv_prctile75 0.483 1.031 -0.1471 1.286 0.0849 0.731
ECG_hrv_geomean_A_ -0.105 1.208 0.5327 1.093 0.1001 0.727
IT_LF_baseline_D 0.518 1.044 -0.2219 0.700 0.5845 0.721
IT_p_Total_baseline 0.507 1.009 -0.2168 0.668 0.6797 0.721
IT_VLF_baseline 0.492 0.990 -0.2217 0.652 0.7285 0.720
ECG_hrv_prctile25 0.244 1.150 -0.4451 0.938 0.5416 0.719
IT_PSD_baseline 0.554 1.095 -0.1829 0.773 0.2875 0.715
ECG_hrv_mean 0.259 0.865 -0.3517 0.931 0.2559 0.714
IT_HF_baseline 0.692 1.254 -0.0466 1.052 0.0555 0.713
ECG_hrv_trimmean25 0.282 0.950 -0.3372 0.967 0.5083 0.711


topLAvar <- univarDe$orderframe$Name[str_detect(univarDe$orderframe$Name,"La_")]
topLAvar <- unique(c(univarDe$orderframe$Name[topvar],topLAvar[1:as.integer(TopVariables/2)]))
finalTable <- univarDe$orderframe[topLAvar,univariate_columns]

theLaVar <- rownames(finalTable)[str_detect(rownames(finalTable),"La_")]

pander::pander(univarDe$orderframe[topLAvar,univariate_columns])
  caseMean caseStd controlMean controlStd controlKSP ROCAUC
La_ECG_RR_window_baseline 0.12566 0.2987 -0.1125 0.2821 0.6434 0.769
La_IT_BRV_baseline -0.14953 0.4231 0.2053 0.2854 0.9658 0.762
La_EDA_Original_mad_D -0.01395 0.0530 0.0239 0.0323 0.3974 0.740
La_ECG_HR_min_div_baseline 0.08643 0.5210 0.3237 0.4734 0.0338 0.734
La_ECG_HR_min_div_mad -0.10207 0.2377 0.0858 0.2795 0.4181 0.734
La_ECG_HR_min_div_std -0.06975 0.2816 0.0875 0.2854 0.3727 0.721
IT_PSD_baseline 0.55441 1.0949 -0.1829 0.7729 0.2875 0.715
ECG_hrv_mean 0.25862 0.8655 -0.3517 0.9306 0.2559 0.714
La_EDA_processed_trimmean25_D 0.21468 0.6566 -0.1355 0.3130 0.4613 0.710
La_EDA_Original_max_D 0.00304 0.0519 -0.0248 0.0265 0.0408 0.708

dc <- getLatentCoefficients(DEdataframe)
fscores <- attr(DEdataframe,"fscore")

theSigDc <- dc[theLaVar]
names(theSigDc) <- NULL
theSigDc <- unique(names(unlist(theSigDc)))


theFormulas <- dc[rownames(finalTable)]
deFromula <- character(length(theFormulas))
names(deFromula) <- rownames(finalTable)

pander::pander(c(mean=mean(sapply(dc,length)),total=length(dc),fraction=length(dc)/(ncol(dataframe)-1)))
mean total fraction
2.88 284 0.778


allSigvars <- names(dc)



dx <- names(deFromula)[1]
for (dx in names(deFromula))
{
  coef <- theFormulas[[dx]]
  cname <- names(theFormulas[[dx]])
  names(cname) <- cname
  for (cf in names(coef))
  {
    if (cf != dx)
    {
      if (coef[cf]>0)
      {
        deFromula[dx] <- paste(deFromula[dx],
                               sprintf("+ %5.3f*%s",coef[cf],cname[cf]))
      }
      else
      {
        deFromula[dx] <- paste(deFromula[dx],
                               sprintf("%5.3f*%s",coef[cf],cname[cf]))
      }
    }
  }
}

finalTable <- rbind(finalTable,univarRAW$orderframe[theSigDc[!(theSigDc %in% rownames(finalTable))],univariate_columns])


orgnamez <- rownames(finalTable)
orgnamez <- str_remove_all(orgnamez,"La_")
finalTable$RAWAUC <- univarRAW$orderframe[orgnamez,"ROCAUC"]
finalTable$DecorFormula <- deFromula[rownames(finalTable)]
finalTable$fscores <- fscores[rownames(finalTable)]

Final_Columns <- c("DecorFormula","caseMean","caseStd","controlMean","controlStd","controlKSP","ROCAUC","RAWAUC","fscores")

finalTable <- finalTable[order(-finalTable$ROCAUC),]
pander::pander(finalTable[,Final_Columns])
  DecorFormula caseMean caseStd controlMean controlStd controlKSP ROCAUC RAWAUC fscores
La_ECG_RR_window_baseline -0.899ECG_RR_window_mean + 1.000ECG_RR_window_baseline 0.12566 0.2987 -0.11253 0.2821 0.64335 0.769 0.522 -1
La_IT_BRV_baseline -0.833IT_BRV_mean + 1.000IT_BRV_baseline -0.14953 0.4231 0.20534 0.2854 0.96581 0.762 0.642 -1
La_EDA_Original_mad_D -1.026EDA_Original_std_D + 1.000EDA_Original_mad_D -0.01395 0.0530 0.02389 0.0323 0.39741 0.740 0.507 0
La_ECG_HR_min_div_baseline + 0.866ECG_RR_window_mean + 1.000ECG_HR_min_div_baseline 0.08643 0.5210 0.32368 0.4734 0.03385 0.734 0.518 -1
La_ECG_HR_min_div_mad + 1.000ECG_HR_min_div_mad -1.003ECG_hrv_std -0.10207 0.2377 0.08581 0.2795 0.41814 0.734 0.576 -1
La_ECG_HR_min_div_std + 1.000ECG_HR_min_div_std -1.020ECG_hrv_std -0.06975 0.2816 0.08752 0.2854 0.37266 0.721 0.579 -1
IT_PSD_baseline 0.55441 1.0949 -0.18292 0.7729 0.28755 0.715 0.715 5
ECG_hrv_mean 0.25862 0.8655 -0.35169 0.9306 0.25586 0.714 0.714 4
La_EDA_processed_trimmean25_D + 1.000EDA_processed_trimmean25_D -0.932EDA_processed_median_D 0.21468 0.6566 -0.13552 0.3130 0.46134 0.710 0.582 -1
La_EDA_Original_max_D -3.511EDA_Original_mean_D + 1.000EDA_Original_max_D + 2.497*EDA_Original_prctile25_D 0.00304 0.0519 -0.02480 0.0265 0.04078 0.708 0.575 -2
IT_BRV_baseline NA -0.35971 0.8468 -0.00106 1.0856 0.08274 0.642 0.642 NA
ECG_RR_window_mean NA -0.07428 0.9814 0.25557 0.9616 0.99919 0.608 0.608 12
EDA_processed_trimmean25_D NA -0.39525 1.3811 -0.61752 1.3648 0.01157 0.582 0.582 NA
ECG_HR_min_div_std NA 0.41350 1.5457 0.63023 1.4649 0.05072 0.579 0.579 NA
ECG_HR_min_div_mad NA 0.37267 1.5023 0.61897 1.4483 0.10526 0.576 0.576 NA
EDA_Original_max_D NA 0.63696 1.3434 0.36718 1.1892 0.00726 0.575 0.575 NA
EDA_Original_prctile25_D NA 0.66082 1.4080 0.37549 1.2295 0.00966 0.573 0.573 NA
EDA_Original_mean_D NA 0.65053 1.3830 0.37869 1.2132 0.01066 0.569 0.569 34
EDA_processed_median_D NA -0.65445 1.3990 -0.51718 1.3120 0.00378 0.530 0.530 NA
ECG_RR_window_baseline NA 0.05886 0.9529 0.11732 0.9276 0.77168 0.522 0.522 NA
ECG_HR_min_div_baseline NA 0.15072 1.0047 0.10247 0.9849 0.67448 0.518 0.518 NA
ECG_hrv_std NA 0.47354 1.4710 0.53182 1.4303 0.03152 0.514 0.514 NA
EDA_Original_mad_D NA 0.40428 1.0948 0.46135 1.1988 0.01686 0.507 0.507 NA
IT_BRV_mean NA -0.25243 1.0855 -0.24788 1.0873 0.27583 0.503 0.503 5
EDA_Original_std_D NA 0.40771 1.0812 0.42646 1.1545 0.02035 0.490 0.490 NA